<!--
*@Author: QWP
*@Description: 派车归队
*@Date 2024-07-15 09:58
-->
<template>
  <a-layout
    class="f-h-100 s-bg-white"
    id="test">
    <u-search-header>
      <u-form
        :model="searchF.model"
        :gutter="16"
        :elements="searchEl"
        :isSearch="true"
        @formChange="getSearch(1)"
        class="f-w-auto">
        <a-col>
          <u-button-space>
            <u-icon-button @click="getSearch(1)">查询</u-icon-button>
            <!-- <u-icon-button itype="export" @click="exportBtn">导出 </u-icon-button> -->
          </u-button-space>
        </a-col>
        <a-col flex="1">
          <a-row justify="end">
            <u-button-space>
              <u-icon-button
                itype="edit"
                @click="toEdit(1)">
                归队编辑
              </u-icon-button>
              <!-- <u-icon-button itype="delete" @click="deleteBtn">删除 </u-icon-button> -->
            </u-button-space>
          </a-row>
        </a-col>
      </u-form>
    </u-search-header>
    <u-table-content class="f-padding-rl24">
      <u-table
        :columns="columns"
        :data="tabData"
        v-model:pagination="pagenation"
        :loading="loading"
        @onSelect="({ selectedRows }) => (tabSelData = selectedRows)"
        @onDblclick="
          (record) => {
            current = record;
            showEdit = true;
          }
        "
        @change="getSearch"></u-table>
    </u-table-content>
  </a-layout>

  <!-- 编辑弹窗 -->
  <Edit
    v-model:open="showEdit"
    wrap-class-name="full-modal"
    :title="dialogTitle"
    :current="current"
    @success="
      (isClose) => {
        current = {};
        showEdit = false;
        getSearch();
      }
    "
    @close="current = {}" />
</template>

<script lang="ts" setup>
import { ref, reactive, computed, onMounted, h, watch } from "vue";
import { useRoute, useRouter, onBeforeRouteUpdate, onBeforeRouteLeave } from "vue-router";
import { useLoginStore, useBaseStore } from "@/store";
import { storeToRefs } from "pinia";
import { _IFormEl } from "@/components/common/form/interface";
import { ColProps, FormItemProps, TableColumnProps } from "ant-design-vue";
import { numberC, messageC, alertC, nullC } from "@/util/common";
import dayjs from "dayjs";
import Edit from "./Edit.vue";
import { cloneDeep } from "lodash";
import { getRegionLabel } from "@/hooks/useRegion";
import * as backApi from "@/service/administration/sendback";

const route = useRoute();
const router = useRouter();
const { userInfo } = storeToRefs(useLoginStore());
const { DISPATCH_STATUS, DISPATCH_SOURCE, APP_STATUS, ISPRINT, deptData } = storeToRefs(useBaseStore());

onMounted(() => {
  getSearch();
});

// 列表筛选
let pagenation = reactive({
  total: 0,
  current: 1,
  pageSize: 50,
  showTotal: (total) => `共 ${total} 条`,
});
const searchF = reactive({
  model: {
    keywords: null,
    dispstatus: "0",
  },
});
const formItemProps: FormItemProps = {
  labelWidth: "60px",
  style: { width: "230px" },
};

const searchEl: Array<_IFormEl> = [
  {
    label: "归队情况",
    key: "dispstatus",
    element: "select",
    formItemProps: formItemProps,
    componentProps: {
      options: [
        { label: "未归队", value: "0" },
        { label: "已归队", value: "1" },
      ],
      allowClear: true,
    },
  },
  {
    label: "关键字",
    key: "keywords",
    element: "input",
    formItemProps: formItemProps,
    componentProps: {
      placeholder: "车牌号",
    },
  },
];

// 列表
const loading = ref<boolean>(false);
const tabSelData = ref<any[]>([]);
const tabData = ref<any[]>([]);
let columns: Array<TableColumnProps> = reactive([
  { dataIndex: "index", title: "序号", width: 50, align: "center", customRender: ({ index }) => (index += 1) },
  {
    dataIndex: "drivername",
    title: "驾驶员",
    width: 150,
    align: "center",
    customRender: ({ text }) => (text == "null" ? "" : nullC(text)),
  },
  {
    dataIndex: "platenumber",
    title: "车牌号",
    width: 150,
    align: "center",
    customRender: ({ text }) => (text == "null" ? "" : nullC(text)),
  },
  // { dataIndex: "vehclescope", title: "用车范围", width: 150, align: 'center' },
  { dataIndex: "departure", title: "出发地", width: 150, customRender: ({ text }) => getRegionLabel(text) },
  { dataIndex: "destination", title: "前往地", width: 150, customRender: ({ text }) => getRegionLabel(text) },
  { dataIndex: "startkilometer", title: "出发里程", width: 150, align: "center" },
  { dataIndex: "endkilometer", title: "归队里程", width: 150, align: "center" },
  { dataIndex: "returnStatus", title: "归队情况", width: 150, align: "center" },
]);

// 列表查询
const getSearch = async (current) => {
  if (current == 1) {
    pagenation.current = current;
  }

  loading.value = true;
  try {
    searchF.current = pagenation.current;
    searchF.size = pagenation.pageSize;
    const data = await backApi.page(searchF);
    if (data.code == 0) {
      tabData.value = data.data.records;
      pagenation.total = data.data.total;
    }
  } catch (error) {}
  loading.value = false;
};

// 编辑
const current = ref<object>({});
let dialogTitle = ref<string>("归队编辑");
let showEdit = ref<boolean>(false);
// type 1 新增   2 修改
const toEdit = (type: number) => {
  if (tabSelData.value.length !== 1) {
    alertC("能且只能选择一条数据进行编辑", "warning");
    return;
  }
  current.value = tabSelData.value[0];
  showEdit.value = true;
};

// 导出
const exportBtn = async () => {
  try {
    loading.value = true;
    const res = await backApi.download({ ...searchF, size: -1 });
    loading.value = false;
  } catch (error) {
    loading.value = false;
  }
};

defineExpose({
  toEdit,
});
</script>

<style lang="scss" scoped>
.icons-list :deep(.anticon) {
  margin-right: 6px;
  font-size: 24px;
}
</style>
